import {request} from "../../request/index"
import regeneratorRuntime from "../../lib/runtime/runtime";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orders: [],
    tabs: [
      {
        id: 0,
        value: '全部',
        isActive: true
      },
      {
        id: 1,
        value: '待付款',
        isActive: false
      },
      {
        id: 2,
        value: '待发货',
        isActive: false
      },
      {
        id: 3,
        value: '退款/退货',
        isActive: false
      },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  onShow: function (options) {
    let pages=getCurrentPages()
    let currentpage=pages[pages.length-1]
    console.log(currentpage.options)
    const {type}=currentpage.options

    this.changeTitleByIndex(type - 1)

    // this.getorders(type)
    // wx.setStorageSync('token', 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo')
    // const token=wx.getStorageSync("token")
    // if(!token){wx.navigateTo({
    //   url: '/pages/auth/index',
    // });return}


  },

  handleTabItemChange(e) {
    console.log(e);
    //获取被点击的标题索引
    let {index} = e.detail;

    this.changeTitleByIndex(index)
  },

  async getorders(type) {
    let res = await request({url: "/my/orders/all",data:{type},method:"GET"})
    this.setData({
      orders: res.orders
    })
  },

  changeTitleByIndex(index) {
    let {tabs} = this.data
    tabs.forEach((v,i) => {
      i === index ? v.isActive = true : v.isActive = false
      this.setData({
        tabs
      })
    })

  }
})